package com.gx.smart.construction.ui.house.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.casic.gx.grpc.common.ComResp;
import com.gx.smart.base.livedata.SingleLiveEvent;
import com.gx.smart.construction.ui.common.viewmodel.CommonViewModel;
import com.gx.smart.construction.ui.house.model.TotalProgress;
import com.gx.smart.lib.event.LoadingMessageEvent;
import com.gx.smart.lib.http.api.ConstructionAppRoomInfoProviderService;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfoResponse;
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionGetRoomCountResponse;

import org.greenrobot.eventbus.EventBus;

/**
 * @author xiaosy
 * @create 2020/8/7
 * @Describe
 **/
public class SmartHomeViewModel extends CommonViewModel {
    private SingleLiveEvent<String> sn;
    private String roomId;
    private MutableLiveData<Boolean> addWayGateSuccess = new MutableLiveData<>();
    private MutableLiveData<Boolean> deleteWayGateSuccess = new MutableLiveData<>();
    private SingleLiveEvent<TotalProgress> progress;

    public SmartHomeViewModel(@NonNull Application application) {
        super(application);
    }


    public LiveData<String> getWayGateDevice(String roomId) {
        EventBus.getDefault().post(new LoadingMessageEvent(true));
        if (sn == null) {
            sn = new SingleLiveEvent<>();
            loadWayGateDevice(roomId);
        } else {
            if (TextUtils.equals(this.roomId, roomId)) {
                sn.setValue(sn.getValue());
            } else {
                loadWayGateDevice(roomId);
            }
        }
        this.roomId = roomId;
        EventBus.getDefault().post(new LoadingMessageEvent(false));
        return sn;
    }

    private void loadWayGateDevice(String roomId) {
        ConstructionAppRoomInfoProviderService.getInstance().getSnByRoomId(roomId, new CallBack<ConstructionAppRoomInfoResponse>() {

            @Override
            public void callBack(ConstructionAppRoomInfoResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    sn.setValue(result.getSn());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });
    }

    private GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse> addWayGateTask;
    public LiveData<Boolean> addWayGate(String roomId, String sn) {
        addWayGateTask = ConstructionAppRoomInfoProviderService.getInstance().deviceBind(roomId, sn, new CallBack<ConstructionAppRoomInfoResponse>() {

            @Override
            public void callBack(ConstructionAppRoomInfoResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    addWayGateSuccess.setValue(true);
                    ToastUtils.showLong("添加网关成功");
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });
        return addWayGateSuccess;
    }

    private GrpcAsyncTask<String, Void, ConstructionAppRoomInfoResponse> deleteWayGateTask;
    public LiveData<Boolean> deleteWayGate(String roomId) {
        deleteWayGateTask = ConstructionAppRoomInfoProviderService.getInstance().deviceUnbind(roomId, new CallBack<ConstructionAppRoomInfoResponse>() {

            @Override
            public void callBack(ConstructionAppRoomInfoResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    deleteWayGateSuccess.setValue(true);
                    ToastUtils.showLong("删除网关成功");
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });

        return deleteWayGateSuccess;
    }

    /**
     * int64 bind_sn_room_count = 2;//绑定网关房间数
     * int64 room_count = 3;//总房间数
     */
    public void getProgress() {
        EventBus.getDefault().post(new LoadingMessageEvent(true));
        if (getTotalProgress().getValue() == null) {
            loadProgress();
        } else {
            changeTotalProgress(getTotalProgress().getValue());
            EventBus.getDefault().post(new LoadingMessageEvent(false));
        }
    }


    private void loadProgress() {
        ConstructionAppRoomInfoProviderService.getInstance().getRoomCount(new CallBack<ConstructionGetRoomCountResponse>() {
            @Override
            public void callBack(ConstructionGetRoomCountResponse result) {
                EventBus.getDefault().post(new LoadingMessageEvent(false));
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    TotalProgress totalProgress = new TotalProgress(result.getBindSnRoomCount(), result.getRoomCount());
//                    progress.setValue(data);
                    changeTotalProgress(totalProgress.getRoomCount() + "/" + totalProgress.getBindRoomCount());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });
    }

    @Override
    protected void onCleared() {
        if(addWayGateTask != null){
            addWayGateTask.cancelTask(true);
        }

        if(deleteWayGateTask != null){
            deleteWayGateTask.cancelTask(true);
        }
    }
}
